<template>
  <div class="app-container">
<!--    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">-->
<!--      <el-form-item label="商品名称" prop="wareSkuId">-->
<!--        <el-input-->
<!--          v-model="queryParams.skuName"-->
<!--          placeholder="请输入商品名称"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="供应商">-->
<!--        <el-select v-model="form.wareSkuId" placeholder="请选择供应商">-->
<!--          <el-option label="区域一" value="shanghai"></el-option>-->
<!--          <el-option label="区域二" value="beijing"></el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item>-->
<!--        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>-->
<!--        <el-button icon="Refresh" @click="resetQuery">重置</el-button>-->
<!--      </el-form-item>-->
<!--    </el-form>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['storage:forewarning:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['storage:forewarning:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['storage:forewarning:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['storage:forewarning:export']"
        >导出</el-button>
      </el-col>
      <el-button
          plain
          @click="handlePurchase"
      >采购</el-button>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="forewarningList">

      <el-table-column label="预警id" align="center" prop="id">
        <template #default="scope">
          <span>{{ scope.$index+1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" prop="skuName" />
      <el-table-column label="库存数" align="center" prop="stock" />
      <el-table-column label="预警值" align="center" prop="forewarningSum" />
      <el-table-column label="预计采购量（预警值三倍）" align="center" prop="forewarningSum">
        <template #default="scope">
          <span>{{ scope.row.forewarningSum*3 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="仓库名称" align="center" prop="name" />
      <el-table-column label="状态" align="center" prop="status">
         <template #default="scope">
          <el-tag v-if="scope.row.status === 1" type="success">待采购</el-tag>
          <el-tag v-else-if="scope.row.status === 2" type="danger">已发起请求</el-tag>
          <el-tag v-else-if="scope.row.status === 3" type="danger">待入库</el-tag>
          <el-tag v-else-if="scope.row.status === 4" type="danger">已入库</el-tag>
        </template>
      </el-table-column>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template #default="scope">-->
<!--          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">开始采购</el-button>-->
<!--&lt;!&ndash;          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>&ndash;&gt;-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog v-model="dialogTableVisible" title="采购" width="800">
      <el-table :data="gridData"   @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column property="商品名称" label="skuName" width="150">
          <template #default="scope">
            <span>{{ scope.row.skuName }}</span>
          </template>
        </el-table-column>
        <el-table-column property="库存数" label="stock" width="200" >
          <template #default="scope">
            <span>{{ scope.row.stock }}</span>
          </template>
        </el-table-column>
        <el-table-column property="预警值" label="forewarningSum" width="200" >
          <template #default="scope">
            <span>{{ scope.row.forewarningSum }}</span>
          </template>
        </el-table-column>
        <el-table-column property="预计采购量（预警值三倍）" label="forewarningSum" width="200">
          <template #default="scope">
            <span>{{ scope.row.forewarningSum*3 }}</span>
          </template>
        </el-table-column>
        <el-table-column property="仓库名称" label="name" width="200" >
          <template #default="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
      </el-table>
      <el-button type="primary" @click="submitForewarning">发起采购</el-button>
    </el-dialog>

    <!-- 添加或修改预警对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="forewarningRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品库存id" prop="wareSkuId">
          <el-input v-model="form.wareSkuId" placeholder="请输入商品库存id" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Forewarning">
import {
  listForewarning,
  getForewarning,
  delForewarning,
  addForewarning,
  updateForewarning, addRequire,

} from "@/api/storage/forewarning";

const { proxy } = getCurrentInstance();

const forewarningList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const wareSkuIds = ref([]);
const gridData = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dialogTableVisible = ref(false)




const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    wareSkuId: null,
    status: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询预警列表 */
function getList() {
  loading.value = true;
  listForewarning(queryParams.value).then(response => {
    forewarningList.value = []
    gridData.value = []
    console.log(response.data.rows)
    response.data.rows.forEach(item => {
      if (item.status != 4){
        forewarningList.value.push(item);
      }
      if (item.status === 1){
        gridData.value.push(item);
        console.log(gridData.value)

      }
    });
    total.value = response.data.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    wareSkuId: null,
    status: null
  };
  proxy.resetForm("forewarningRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  wareSkuIds.value = []
  selection.forEach(item => {
    wareSkuIds.value.push(item.wareSkuId)
  })
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加预警";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getForewarning(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改预警";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["forewarningRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateForewarning(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addForewarning(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除预警编号为"' + _ids + '"的数据项？').then(function() {
    return delForewarning(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('storage/forewarning/export', {
    ...queryParams.value
  }, `forewarning_${new Date().getTime()}.xlsx`)
}

/** 采购操作 */
function handlePurchase() {
  dialogTableVisible.value = true

}

/**
 * 采购
 */
function submitForewarning() {
  console.log(wareSkuIds.value)
  addRequire(wareSkuIds.value).then(res=>{
    proxy.$modal.msgSuccess("采购成功");
    dialogTableVisible.value = false
    getList();
  })
}

getList();
</script>
